Add saveModel stub
[f432xdd.git] / java / Graph Editor / src / models / GraphModel.java
blob4c05ba9caf2043604fb1d428466d6d935e62bcd8
1 package models;
3 import java.io.*;
4 import java.util.List;
5 import java.util.ArrayList;
6 import java.util.Observable;
7 import java.util.Observer;
8 import java.util.Scanner;
11 public class GraphModel extends Observable implements Observer{
12 private List<GraphEdge> edges;
13 private List<GraphVertex> vertexes;
16 public GraphVertex[] getVertexes(){
17 GraphVertex[] a = new GraphVertex[vertexes.size()];
18 return vertexes.toArray(a);
20 public GraphEdge[] getEdges(){
21 GraphEdge[] a = new GraphEdge[edges.size()];
22 return edges.toArray(a);
25 public GraphModel(){
26 edges = new ArrayList<GraphEdge>();
27 vertexes = new ArrayList<GraphVertex>();
30 public synchronized void setChanged() {
31 super.setChanged();
32 notifyObservers();
35 public void addEdge(GraphEdge ... edge){
36 for(int i=0; i < edge.length; i++){
37 edges.add(edge[i]);
38 edge[i].addObserver(this);
40 setChanged();
43 public void addGraph(GraphVertex ... vertex){
44 for(int i=0; i < vertex.length; i++){
45 vertexes.add(vertex[i]);
46 vertex[i].addObserver(this);
48 setChanged();
51 public void removeEdge(GraphEdge ... edge){
52 for(int i=0; i < edge.length; i++){
53 edges.remove(edge[i]);
54 edge[i].deleteObserver(this);
56 setChanged();
58 public void removeGraph(GraphVertex ... vertex){
59 for(int i=0; i < vertex.length; i++){
60 vertexes.remove(vertex[i]);
61 vertex[i].deleteObserver(this);
63 setChanged();
66 public GraphVertex getGraphByCoordinate(int x, int y){
67 for(int i=0; i < vertexes.size(); i++){
68 if(vertexes.get(i).isAt(x, y))
69 return vertexes.get(i);
71 return null;
74 public void saveModel() throws IOException {
75 PrintWriter pw = new PrintWriter(new FileWriter("file.graph"));
76 pw.println(vertexes.size() + "," + edges.size());
80 @Override
81 public void update(Observable arg0, Object arg1) {
82 if(vertexes.contains(arg0) || edges.contains(arg0)){
83 this.setChanged();
88 //File IO Code
89 public void writeToFile(File file) throws Exception
91 FileOutputStream fos = new FileOutputStream(file);
92 OutputStreamWriter out = new OutputStreamWriter(fos, "UTF-8");
94 out.write(vertexes.size() + " " + edges.size() + "\r\n");
95 for(int i=0; i < vertexes.size(); i++)
96 out.write(vertexes.get(i).getX() + " " + vertexes.get(i).getY() + " " + vertexes.get(i).getWidth() + " " + vertexes.get(i).getHeight() +" "+ vertexes.get(i).getName() + "\r\n");
97 for(int i=0; i < edges.size(); i++)
98 out.write(vertexes.indexOf(edges.get(i).getEdge(false)) + " " + vertexes.indexOf(edges.get(i).getEdge(true)) + "\r\n");
99 out.flush();
100 out.close();
101 fos.flush();
102 fos.close();
107 private static GraphVertex[] readVertexes(int count, Scanner s)
109 GraphVertex[] vertexes = new GraphVertex[count];
110 for(int i=0; i < count; i++)
111 vertexes[i] = new GraphVertex(s);
112 return vertexes;
116 public static GraphModel fromFile(File file) throws Exception
118 GraphModel model = new GraphModel();
119 Scanner scanner = new Scanner(file, "UTF-8");
121 int numKnopen = scanner.nextInt();
122 int numKanten = scanner.nextInt();
123 scanner.nextLine();
125 GraphVertex[] vertexes = readVertexes(numKnopen,scanner);
127 model.addEdge(readKanten(scanner, numKanten, vertexes));
128 model.addGraph(vertexes);
130 return model;
132 private static GraphEdge[] readKanten(Scanner scanner, int numKanten,
133 GraphVertex[] vertexes) {
134 GraphEdge[] edges = new GraphEdge[numKanten];
135 for (int i = 0; i < numKanten; i++) {
136 int obj1 = scanner.nextInt();
137 int obj2 = scanner.nextInt();
138 edges[i] = new GraphEdge(vertexes[obj1], vertexes[obj2]);
139 if(i != (numKanten-1)){
140 scanner.nextLine();
143 return edges;
147 public static GraphModel fromFile(String fileName) throws Exception
149 File file = new File(fileName);
150 return fromFile(file);